package View;

/**
 * Classe base para tudo o que sera desenhado na tela.
 * 
 * @author Edjane Catolle, Igor Henrique, Luiz Augusto, Vladmir Chicarolli
 * @version 1.0.0
 * @since 05/31/2011
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public abstract class BaseView  extends JPanel
{
	private static JFrame _frame;	
	protected Image buffer;
	private Graphics gBuffer;
	private Point pos;	
	public long timeSplashScreen = 5000;
	public long timeMainMenu = 3000;
	public long screenTime;
	
	/**
	 * Inicializa a janela
	 */
	public BaseView()
	{
		pos = new Point(0, 0);
		screenTime = System.currentTimeMillis();
		
		this.setPreferredSize(new Dimension(1000,700));
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		FrameManager().setContentPane(this);
		FrameManager().setTitle("Save The Sheep!");
		FrameManager().setBackground(Color.BLACK);
		FrameManager().pack();
		FrameManager().setVisible(true);
		FrameManager().setSize(this.getSize());
		FrameManager().setLocationRelativeTo(null);
		FrameManager().setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		FrameManager().createBufferStrategy(2);
		
		gBuffer = FrameManager().getBufferStrategy().getDrawGraphics();	
	}
		
	public void Update()
	{			
		DrawHandler();
	}
		
	/**
	 * Desenha a imagem desejada na tela.
	 */
	protected void DrawBuffer()
    {		
		BufferStrategy bs = FrameManager().getBufferStrategy();
		gBuffer = bs.getDrawGraphics();					
    	gBuffer.drawImage(buffer, pos.x, pos.y, null);
    	bs.show();
    }
		
	/**
	 * Limpa o buffer.
	 */
	public void ClearBuffer()
	{
		BufferStrategy bs = FrameManager().getBufferStrategy();
		gBuffer = bs.getDrawGraphics();		
		gBuffer.clearRect(0, 0, FrameManager().getWidth(), FrameManager().getHeight());
		bs.show();
	}
	
	/**
	 *Desenha a imagem a partir do centro.
	 * @param dif - A coordenada para centralizar a imagem na tela. (0, 0) vai colocar a imagem
	 * exatamente no centro da tela.
	 */
	protected void CenterImage(Point dif)
	{
		pos.x = ((getWidth() / 2) - (buffer.getWidth(null) / 2)) + dif.x;
		pos.y = ((getHeight() / 2) - (buffer.getHeight(null) / 2)) + dif.y;
	}
	
	/**
	 * Configura uma posicao para a imagem na tela.
	 * @param dif - A coordenada desejada para desenhar a imagem.
	 */
	public void CustomImage(Point dif)
	{
		pos.setLocation(dif);
	}
	
	/**
	 * "Handler" para as views a serem desenhadas.
	 * Design Pattern "TEMPLATE METHOD".
	 */
	public abstract void DrawHandler();
	
	/**
	 * Gerengia o frame para que tenhamos somente uma instancia do JFrame.
	 * Design Pattern "SINGLETON".
	 * @return JFrame - retorna o frame instanciado.
	 */
	public static JFrame FrameManager()
	{
		if(_frame == null)
		{
			_frame = new JFrame();
		}
		return _frame;
	}	
}
